package com.teemor.blog.service.impl;


import com.teemor.blog.bean.condition.TbArticleExtendCondition;
import com.teemor.blog.bean.entity.TbArticleExtend;
import com.teemor.blog.mapper.TbArticleExtendMapper;
import com.teemor.blog.service.IArticleExtendService;
import java.util.List;

import com.teemor.common.bean.utils.UUIDUtils;
import com.teemor.common.bean.utils.redis.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: zhoulk
 * @Description: 原子服务实现
 * @date: 2019/11/26 14:50:14
 */
@Service
@Slf4j
public class ArticleExtendServiceImpl implements IArticleExtendService {

    @Autowired
    private TbArticleExtendMapper tbArticleExtendMapper;

    @Override
    public int countArticleExtend(TbArticleExtendCondition condition) {
        return tbArticleExtendMapper.countArticleExtendByCondition(condition);
    }

    @Override
    public List<TbArticleExtend> qryArticleExtends(TbArticleExtendCondition condition) {

        return tbArticleExtendMapper.selectArticleExtendByCondition(condition);
    }

    @Override
    public TbArticleExtend qryArticleExtendById(Integer atcId) {
        return tbArticleExtendMapper.selectArticleExtendByAtcId(atcId);
    }

    @Override
    public int addArticleExtend(TbArticleExtend inParam) {
        return tbArticleExtendMapper.insertArticleExtend(inParam);
    }

    @Override
    public int updArticleExtendById(TbArticleExtend inParam) {
        return tbArticleExtendMapper.updateArticleExtendByAtcId(inParam);
    }

    @Override
    public Integer praise(Integer atcId, Integer type, String ip) {
        ip = StringUtils.isBlank(ip) ? UUIDUtils.generateUuid() : ip;
        boolean access = RedisUtils.simpleLimit(getClass().getSimpleName() + "_" + ip + "_" + atcId, 60 * 60 , 1) > -1;
        if (!access) {
            log.warn("重复点赞：atcId:{}, type:{}, ip:{}", atcId, type, ip);
            return -1;
        }
        TbArticleExtend articleExtend = tbArticleExtendMapper.selectArticleExtendByAtcId(atcId);
        if (type == 1) {
            articleExtend.setPraiseNum(articleExtend.getPraiseNum() + 1);
        } else if (type == -1) {
            articleExtend.setNoPraiseNum(articleExtend.getNoPraiseNum() + 1);
        }
        return tbArticleExtendMapper.updateArticleExtendByAtcId(articleExtend);
    }
}